package com.base.hong.ui

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.base.hong.R
import com.base.hong.databinding.ActivityWebViewBinding
import com.base.hong.ext.doubleClick
import com.base.hong.ui.base.BaseVMActivity
import com.base.hong.ui.web.BaseWebInterface
import com.htt.base_library.base.NoViewModel
import com.just.agentweb.AbsAgentWebSettings
import com.just.agentweb.AgentWeb
import com.just.agentweb.IAgentWebSettings
import com.just.agentweb.WebChromeClient
import com.base.hong.ui.web.BaseWebJsCallback

class WebViewActivity : BaseVMActivity<NoViewModel, ActivityWebViewBinding>(), BaseWebJsCallback {

    private var mAgentWeb: AgentWeb? = null
    private var mTitle: String? = null
    private var mUrl = ""


    override fun getTitleBar(): View? {
        return mViewBinding.includeToolbar?.let {
            it.toolbarImgBack.doubleClick {
                if (mAgentWeb?.webCreator?.webView?.canGoBack() == true) {
                    mAgentWeb?.back()
                } else {
                    finish()
                }
            }
            it.toolbar.setBackgroundColor(Color.parseColor("#00FFFFFF"))
            it.toolbar
        }
    }

    override fun initView(intent: Intent?, savedInstanceState: Bundle?) {

        mUrl = getIntent().getStringExtra("url").toString()
        mTitle = getIntent().getStringExtra("title")
        mAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(mViewBinding.webview, LinearLayout.LayoutParams(-1, -1))
            .useDefaultIndicator(ContextCompat.getColor(this, R.color.font_blue_color), 1)
//            .setAgentWebWebSettings(getSettings())//设置 IAgentWebSettings。
            .setWebChromeClient(mWebChromeClient)
            .setAgentWebWebSettings(object : AbsAgentWebSettings() {
                override fun bindAgentWebSupport(agentWeb: AgentWeb?) {
                }

                override fun toSetting(webView: WebView?): IAgentWebSettings<*> {
                    val iAgentWebSettings = super.toSetting(webView)
                    iAgentWebSettings.webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
                    return iAgentWebSettings
                }
            })
            .createAgentWeb()
            .ready()
            .go(mUrl)
        mAgentWeb?.jsInterfaceHolder?.addJavaObject(
            "android",
            BaseWebInterface(mAgentWeb, this, this)
        )

    }

    private var mWebChromeClient: WebChromeClient = object : WebChromeClient() {
        override fun onReceivedTitle(view: WebView, title: String) {
            if (!TextUtils.isEmpty(mTitle)) {
                mViewBinding.includeToolbar.toolbarTvTitle.text = mTitle
            } else {
                mViewBinding.includeToolbar.toolbarTvTitle.text = title
            }
            super.onReceivedTitle(view, title)
        }

        override fun onProgressChanged(view: WebView, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
        }
    }

    override fun onPause() {
        mAgentWeb?.webLifeCycle?.onPause()
        super.onPause()
    }

    override fun onResume() {
        mAgentWeb?.webLifeCycle?.onResume()
        super.onResume()
    }

    override fun onDestroy() {
        mAgentWeb?.webLifeCycle?.onDestroy()
        super.onDestroy()
    }


}